package com.lxh.mp.controller;

import com.google.common.collect.Lists;
import com.lxh.mp.utils.JsonUtils;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.WxMpTemplateMsgService;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * created by lanxinghua@2dfire.com on 2020/2/21
 * 公众号其他服务测试
 */
@AllArgsConstructor
@Controller
@RequestMapping("/wx/other/{appid}")
public class OtherController {
    private static Logger logger = LoggerFactory.getLogger(OtherController.class);
    private final WxMpService wxService;

    /**
     * 发送模板消息
     * @param templateId
     *
     * {{first.DATA}}
     *
     * {
     *   "touser":"oDy4FxM8oaxRohalYBEp8QtIRGZc",
     *   "template_id":"lZHhB69osSe-4YaVwkXs4nB9vMpDpD3Bj-PM1SuMpHs",
     *   "url": "http://www.baidu.com",
     *   "data":{
     *     "first": {
     *       "value":"恭喜你购买成功！",
     *       "color":"#173177"
     *     }
     *   }
     * }
     * @return
     */
    @RequestMapping("/sendTemplate")
    public void sendTemplate(@PathVariable String appid,
                               @RequestParam(required = false) String templateId) {
        // 切换公众号
        if (!this.wxService.switchover(appid)) {
            throw new IllegalArgumentException(String.format("未找到对应appid=[%s]的配置，请核实！", appid));
        }
        try {
            WxMpTemplateMsgService templateMsgService = wxService.getTemplateMsgService();
            WxMpTemplateMessage templateMessage = new WxMpTemplateMessage();

            List<WxMpTemplateData> list = Lists.newArrayList();
            WxMpTemplateData data = new WxMpTemplateData();
            data.setName("first");
            data.setValue("恭喜你购买成功！");
            data.setColor("#173177");
            list.add(data);
            templateMessage.setData(list);

            templateMessage.setToUser("oDy4FxM8oaxRohalYBEp8QtIRGZc");
            templateMessage.setUrl("http://www.baidu.com");
            templateId = StringUtils.isEmpty(templateId) ? "lZHhB69osSe-4YaVwkXs4nB9vMpDpD3Bj-PM1SuMpHs" : templateId;
            templateMessage.setTemplateId(templateId);

            String result = templateMsgService.sendTemplateMsg(templateMessage);
            logger.info("发送模板:" + result);
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
    }
}
